package com.baizhi.service.impl;

import com.baizhi.service.LogService;
import com.baizhi.vo.LogBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 获取日志
 *
 * @author Administrator
 */
@Service
@Slf4j
public class LogServiceImpl implements LogService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public PageImpl<LogBean> getLogs(Map<String, String> map) {
        String sPage = "";
        String sLimit = "";
        if (CollectionUtils.isEmpty(map)) {
            sPage = map.get("page");
            sLimit = map.get("limit");
        }
        int page = StringUtils.isNotBlank(sPage) ? Integer.parseInt(sPage) : 1;
        int limit = StringUtils.isNotBlank(sLimit) ? Integer.parseInt(sLimit) : 1;
        //分页
        log.info("page:{}", page);
        log.info("limit:{}", limit);
        Pageable pageable = PageRequest.of(page, limit);
        Criteria criteria = new Criteria();
//        criteria.andOperator(
//                Criteria.where("").is("")
//        );
//        criteria.and("").is("");
        Query query = new Query(criteria);
        query.with(pageable);
        long count = mongoTemplate.count(query, LogBean.class);
        List<LogBean> result = mongoTemplate.find(query, LogBean.class);
//        total
        
        return new PageImpl<LogBean>(result, pageable, count);
    }
}
